package gold.digger;

/**
 * Created by fanzhenyu02 on 2020/3/1.
 */
public class LC116 {

    class Node {
        public int val;
        public Node left;
        public Node right;
        public Node next;

        public Node() {
        }

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, Node _left, Node _right, Node _next) {
            val = _val;
            left = _left;
            right = _right;
            next = _next;
        }
    }

    ;


    public Node connect(Node root) {
        if (root == null) {
            return null;
        }

        Node curRoot = root;
        // 使用next 做广度优先遍历
        while (curRoot.left != null) {
            Node curNode = curRoot;
            while (curNode != null) {
                curNode.left.next = curNode.right;
                if (null != curNode.next) {
                    curNode.right.next = curNode.next.left;
                }
                curNode = curNode.next;
            }

            // 开始遍历下一层
            curRoot = curRoot.left;
        }

        return root;
    }

    public static void main(String[] args) {
        LC116 an = new LC116();
//
    }
}
